define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'bootstrap-table-editable'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'box/stock/index',
                    add_url: 'box/stock/add',
                    edit_url: 'box/stock/edit',
                    del_url: 'box/stock/del',
                    multi_url: 'box/stock/multi',
                    table: 'box',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-dialog").data("area", ['90%', '90%']);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'box_id',
                sortName: 'box_id',
                searchFormVisible: false,
                searchFormTemplate: 'customformtpl',
                queryParams: function (params) {
                    var options = table.bootstrapTable('getOptions');
                    var filter = JSON.parse(params.filter);
                    filter.status = 1;
                    params.filter = JSON.stringify(filter);

                    return params;
                },
                columns: [
                    [
                        {field: 'box_no', title: __('Box_no')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'area', title: __('Area'), formatter: Controller.api.formatter.city_info},
                        {field: 'room', title: __('Room'), formatter: Controller.api.formatter.address_info},
                        {field: 'contacts', title: __('Contacts')},
                        {field: 'sex', title: __('性别')},
                        {field: 'telephone', title: __('Telephone')},
                        {field: 'stock',sortable: true, title: __('商品剩余库存')},
                        {field: 'worth',sortable: true, title: __('剩余商品价值')},
                        {field: 'cost', title: __('成本-收益')},
                        {field: 'place_time_start', title: __('Place_time_start'), operate: 'RANGE',sortable: true, addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.custom
                        }
                    ]
                ]
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        replen: function () {
            var table2 = $("#table");
            var ids = table2.attr('ids');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'replen/goodslist?ids=' + ids,
                    add_url: 'replen/add',
                    edit_url: 'replen/edit',
                    del_url: 'replen/del',
                    multi_url: 'replen/multi',
                    table: 'order',
                }
            });


            // 初始化表格
            table2.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'replen_goods_id',
                sortName: 'replen_goods_id',
                toolbar: '#toolbar1',
                showRefresh: true,
                commonSearch: false,
                columns: [
                    [
                        {field: 'goods_id', title: __('Goods_id')},
                        {field: 'goods_title', title: __('Goods_title'), formatter: Controller.api.formatter.goods_info, operate: false, align: 'left'},
                        {field: 'brand_title', title: __('Brand_title'), operate: false},
                        {field: 'cate_title', title: __('Cate_title'), operate: false},
                        {field: 'purchase_price', title: __('Purchase_price'), operate: false},
                        {field: 'goods_price', title: __('Goods_price'), operate: false},
                        {field: 'selling_price', title: __('Selling_price'), operate: false},
                        {field: 'goods_weight', title: __('Goods_weight'), operate: false},
                        {
                            field: 'buy_num',
                            title: __('补货数量'),
                            editable: {
                                type: 'text'
                            }
                        },
                        {field: 'createtime', title: __('添加时间'), operate: false},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'goods_custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.goods_custom
                        },
                    ]
                ],
                onEditableSave: function (field, row, oldValue, $el) {
                    $.ajax({
                        type: "post",
                        url: "replen/editnum?ids=" + ids,
                        data: row,
                        dataType: 'JSON',
                        success: function (data, status) {
                            if (status == "success") {
                                Toastr.success('操作成功');
                            }
                        },
                        error: function () {
                            Toastr.success('编辑失败');
                        },
                    });
                }
            });

            $(document).on("click", "[data-id].btn-refresh", function (e) {
                table2.bootstrapTable('refresh', {});
            });

            $(document).on("click", ".btn-addgoods", function (e) {
                Backend.api.open('replen/addgoods?ids=' + ids, __('添加补货商品'), {
                    callback: function (value) {
                        table2.bootstrapTable('refresh', {});
                    }
                });
            });

            $(table2).on("click", "[data-id].btn-delgoods", function (e) {
                e.preventDefault();
                var id = $(this).data("id");

                var that = this;
                Layer.confirm(
                        __('确定删除该补货商品吗？'),
                        {icon: 3, title: __('确定删除该补货商品吗？'), shadeClose: true},
                        function (index) {
                            $.post('replen/delgoods', {id: id, ids: ids}, function (res) {
                                table2.bootstrapTable('refresh', {});
                            });
                            Layer.close(index);
                        }
                );
            });

            Form.api.bindevent($("form[role=form]"), function (data, ret) {
                //这里是表单提交处理成功后的回调函数，接收来自php的返回数据
                Fast.api.close(data);//这里是重点
            }, function (data, ret) {
            });

        }, addgoods: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'replen/addgoods?ids=' + Fast.api.query('ids'),
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: 'goods',
                }
            });

            var table1 = $("#table");
            // 初始化表格
            table1.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_id',
                queryParams: function (params) {
//                        var options = table.bootstrapTable('getOptions');
                    //这里可以追加搜索条件
//                        var filter = JSON.parse(params.filter);
//                        var op = JSON.parse(params.op);
//                        filter.referee_id=Fast.api.query('ref');
//                        params.filter = JSON.stringify(filter);
//                        params.op = JSON.stringify(op);
//                        return params;
                    var options = table1.bootstrapTable('getOptions');
                    var filter = JSON.parse(params.filter);
                    filter.status = 1;
                    params.filter = JSON.stringify(filter);

                    return params;
                },
                columns: [
                    [
                        {field: 'goods_id', title: __('Goods_id')},
                        {field: 'goods_title', title: __('Goods_title'), formatter: Controller.api.formatter.goods_info, operate: 'like', align: 'left'},
                        {field: 'brand_title', title: __('Brand_title'), operate: false},
                        {field: 'cate_title', title: __('Cate_title'), operate: false},
                        {field: 'cate_id', title: __('Cate_title'), visible: false, searchList: $.getJSON('goods/cateList')},
                        {field: 'brand_id', title: __('Brand_title'), visible: false, searchList: $.getJSON('goods/brandList')},
                        {field: 'goods_weight', title: __('Goods_weight'), operate: false},
                        {field: 'purchase_price', title: __('Purchase_price'), operate: false},
                        {field: 'selling_price', title: __('Selling_price'), operate: false},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'add_goods_custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.add_goods_custom
                        },
                    ]
                ]
            });



            $(table1).on("click", "[data-id].btn-addboxgoods", function (e) {
                e.preventDefault();


                var id = $(this).data("id");
                var ids = $(this).data("ids");
                $.post('replen/addreplengoods', {goods_id: id, ids: ids}, function (res) {
                    table1.bootstrapTable('refresh', {});
                });
            });

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                address_info: function (value, row, index) {
                    if (row.quarters_id == 0) {
                    } else {
                        var html = row.quarters_name + '-' + row.unit_name + '-' + row.room_name;
                        return html;
                    }

                },
                city_info: function (value, row, index) {
                    if (row.province_name == null) {
                    } else {
                        var html = row.province_name + '-' + row.city_name + '-' + row.area_name;
                        return html;
                    }

                },
                add_goods_custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var html = '<a class="text-success btn-addboxgoods"  style="cursor:pointer" data-url="replen/addreplengoods" data-ids="' + Fast.api.query('ids') + '" data-id="' + row.goods_id + '"><b>添加</b></a>';

                    return html;
                },
                goods_custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var html = '';
                    html += '<a class="text-danger btn-delgoods"  style="cursor:pointer" data-url="replen/delgoods" data-id="' + row.goods_id + '"><b>删除</b></a>';

                    return html;
                },
                custom: function (value, row, index) {

                    var html = '<a href="box/stock/replen?ids=' + row.user_box_id + '" class="text-info btn-bfjl btn-dialog" title="补货" style="cursor:pointer"  data-id="' + row.user_box_id + '"><b>补货</b></a>';
                    html += '|<a href="box/stock/detail?ids=' + row.user_box_id + '" class="text-success btn-dialog" title="盒子详情" style="cursor:pointer"  data-id="' + row.user_box_id + '"><b>盒子详情</b></a>';

                    return html;
                }
            },
        }
    };
    return Controller;
});
